import {
    Controller,
    Get,
    Param,
    Post,
    Put,
    UseGuards,
    Request,
} from '@nestjs/common';
import { JwtAuthGuard } from 'src/modules/auth/guard/jwt-auth.guard';
import { MessageService } from 'src/service/messsge.service';
import { ScoketGateway } from 'src/websocket/websocket.getway';

@Controller('message')
export class MessageController {
    constructor(
        private readonly messageService: MessageService,
        private readonly ws: ScoketGateway,
    ) { }

    @Post('order/:hotelId/:orderId/:content')
    async createOrderMessage(
        @Param('hotelId') hotelId: number,
        @Param('orderId') orderId: number,
        @Param('content') content: string,
    ) {
        const data = await this.messageService.createOrderMessage(
            hotelId,
            orderId,
            content,
        );
        const buffer = JSON.stringify(data);

        this.ws.server.on('connection', function (socket) {
            // console.log(data);
            socket.send(buffer);
        });

        return data;
    }

    @Post('room/:hotelId/:roomId/:content')
    async createRoomMessage(
        @Param('hotelId') hotelId: number,
        @Param('roomId') roomId: number,
        @Param('content') content: string,
    ) {
        const data = await this.messageService.createRoomMessage(
            hotelId,
            roomId,
            content,
        );
        // this.ws.server.emit('ad', { data: data });
        return data;
    }

    @Post('system/:hotelId/:content')
    async createSystemMessage(
        @Param('hotelId') hotelId: number,
        @Param('content') content: string,
    ) {
        return this.messageService.createSystemMessage(hotelId, content);
    }

    @Post('hotel/:hotelId/:content/:confirmRequire')
    async createHotelMessage(
        @Param('hotelId') hotelId: number,
        @Param('content') content: string,
        @Param('confirmRequire') confirmRequire: number,
    ) {
        return this.messageService.createHotelMessage(
            hotelId,
            content,
            confirmRequire,
        );
    }

    @UseGuards(JwtAuthGuard)
    @Put('confirmRequire/:id')
    async confirmMessage(@Request() req, @Param('id') id: number) {
        return this.messageService.confirmRequire(req.user.userId, id);
    }

    @UseGuards(JwtAuthGuard)
    @Put('overRead/:id')
    async overReadMessage(@Request() req, @Param('id') id: number) {
        return this.messageService.confirmRead(req.user.userId, id);
    }

    @Get('messageChannel/:messageChannel/:pageNo/:pageSize')
    async findByMesssageChannel(
        @Param('messageChannel') messageChannel: number,
        @Param('pageNo') pageNo: number,
        @Param('pageSize') pageSize: number,
    ) {
        return this.messageService.findMessage(messageChannel, pageNo, pageSize);
    }

    @Get('roomId/:roomId/:pageNo/:pageSize')
    async findByRoomId(
        @Param('roomId') roomId: number,
        @Param('pageNo') pageNo: number,
        @Param('pageSize') pageSize: number,
    ) {
        return this.messageService.findMessageByRoomId(roomId, pageNo, pageSize);
    }

    @Get()
    async findAll() {
        const data = await this.messageService.findAll();

        const buffer = JSON.stringify(data);

        this.ws.server.on('connection', function (socket) {
            // console.log(data);
            // socket.send(buffer);
            socket.send(buffer);
        });
        // this.ws.onCreateMessage(123);

        return data;
    }
}
